import { reactive, markRaw } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import { getMaterialList } from '@/apis/system/config/material'
import type { IPageMaterialParams } from '@/apis/system/config/model/materialModel'
import type { BaseTableProps } from '@/components/BaseTable/index'
import type { BasicPagingListParams } from '@/apis/basicModel'
import type { SearchOptionsType, TableFieldsSettingType } from '@/components/CPageTable'
import { ColumnSettingTypeEnum } from '@/components/CPageTable'
export const useList = () => {
  // 导入因子弹框
  // 搜索字段及其他
  const searchForm = reactive<IPageMaterialParams>({
    materialName: undefined,
    unit: undefined
  })
  // 搜索配置
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '能源/燃料名称',
      field: 'materialName',
      type: 'input'
    },
    {
      name: '单位',
      field: 'unit',
      type: 'select',
      fixedOption: 'modelUnitAllOption'
    }
  ])

  // 表格配置
  const tableOptions: BaseTableProps = {
    topBorder: false,
    loadData,
    selection: false
  }
  function loadData(parameter: BasicPagingListParams) {
    const params: IPageMaterialParams & BasicPagingListParams = Object.assign(
      handleParams(),
      parameter
    )
    return getMaterialList(params)
  }
  // 处理参数
  const handleParams = (): IPageMaterialParams => {
    // 参数
    let params = {
      materialName: searchForm.materialName,
      unit: searchForm.unit
    }
    params = removeUndefinedParams(params)
    return params
  }

  const tableFieldsSetting: TableFieldsSettingType[] = [
    {
      title: '能源/燃料',
      slot: 'nameSlot',
      minWidth: 220
    },
    {
      title: '能源/燃料单位',
      slot: 'unitSlot',
      width: 160
    },
    {
      title: '能源/燃料描述',
      field: 'materialDesc',
      minWidth: 150
    },
    {
      title: '启用状态',
      slot: 'statusSlot',
      width: 90
    },
    {
      title: '更新人',
      field: 'modifyBy',
      width: 120
    },
    {
      title: '更新时间',
      field: 'updateTime',
      width: 180
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 150,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  return {
    // 头部右侧筛选项配置
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting
  }
}
